package com.aliyun.messagemgt.application.service.Impl;

import com.aliyun.messagemgt.common.dto.send.MsgMiddleResult;
import com.aliyun.messagemgt.common.dto.send.MsgRecordVO;
import com.aliyun.messagemgt.common.dto.send.Receiver;
import com.aliyun.messagemgt.application.service.MsgHandleLogService;
import com.aliyun.messagemgt.domain.repository.MsgHandleLogRepository;
import com.aliyun.messagemgt.dto.MsgHandleLog;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IDEA
 *
 * @author:zhangxingxing 0027012120
 * @date:2019/1/23
 * @time:9:46
 */
@Service
public class MsgHandleLogServiceImpl implements MsgHandleLogService {

    private static final Logger logger = LoggerFactory.getLogger(MsgHandleLogServiceImpl.class);

    @Autowired
    private MsgHandleLogRepository msgHandleLogRepository;

    @Override
    public int batchMsgHandleLogInsert(List<MsgRecordVO> msgRecordVos) {
        logger.info(msgRecordVos.toString());
        boolean falg = false;
        for (MsgRecordVO msgRecordVO:msgRecordVos) {
            List<MsgMiddleResult> results = msgRecordVO.getResults();
            if(!CollectionUtils.isEmpty(results)){
                for (MsgMiddleResult msgMiddleResult:results) {
                    List<Receiver> receivers = msgMiddleResult.getReceivers();
                    if(!CollectionUtils.isEmpty(receivers)){
                        falg = true;
                        break;
                    }
                }
            }
        }
        if(falg){
            return msgHandleLogRepository.batchMsgHandleLogInsert(msgRecordVos);
        }else {
            return 0;
        }
    }

    @Override
    public void updateByMsgId(MsgHandleLog msgHandleLog) {
        msgHandleLogRepository.updateByMsgId(msgHandleLog);
    }

    @Override
    public void updatePushResult(MsgHandleLog msgHandleLog, List<Receiver> receiverList) {
        msgHandleLogRepository.updatePushResult(msgHandleLog,receiverList);
    }
}
